//
//  MWDropDownSelectionView.m
//  JYG_Sales
//
//  Created by Mike on 15/10/27.
//  Copyright (c) 2015年 WeShare. All rights reserved.
//

#define TableWidth       ImageWidth-10

#import "MWDropDownSelectionView.h"

@implementation MWDropDownSelectionView
{
    UIImageView* img;
    UITableView* dropDownTab;
    float  ImageWidth;
    NSArray* listArr;
    NSInteger celIndex;
}

- (id)initWithFrame:(CGRect)frame withArray:(NSArray*)array withIndex:(NSInteger)index withImageWidth:(float)imageWidth
{
    self = [super initWithFrame:frame];
    if (self) {
        ImageWidth = imageWidth;
        self.frame = kScreenBounds;
        if (array) {
            listArr = array;
        }else{
            listArr = [NSArray array];
        }
        
        celIndex = index;
        [self setBackgroundColor:[UIColor clearColor]];
        
        UIView* bgView = [[UIView alloc]initWithFrame:kScreenBounds];
        [bgView setBackgroundColor:[UIColor clearColor]];
        [self addSubview:bgView];
        
        UIImage* image = [UIImage imageNamed:@"claim_rectangle"];
        img = [[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-ImageWidth)/2, 56, ImageWidth, 0)];
        img.image=image;//[image stretchableImageWithLeftCapWidth:22 topCapHeight:30];//拉伸图片
        img.userInteractionEnabled = YES;
        [img setBackgroundColor:[UIColor clearColor]];
        [self addSubview:img];
        
        dropDownTab = [[UITableView alloc]initWithFrame:CGRectMake(5, 8, TableWidth, 2) style:UITableViewStylePlain];
        [dropDownTab setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [dropDownTab setBackgroundColor:[UIColor clearColor]];
        dropDownTab.scrollEnabled = NO;
        dropDownTab.delegate= self;
        dropDownTab.dataSource = self;
        [img addSubview:dropDownTab];
        
        [self showAnimate];
        
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeSelf)];
        [bgView addGestureRecognizer:tap];
    }
    
    return self;
}

- (void)showInWindow
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)showAnimate
{
    [img setFrame:CGRectMake((SCREEN_WIDTH-ImageWidth)/2, 57, ImageWidth, 100)];
    [dropDownTab setFrame:CGRectMake(5, 8, TableWidth, 92)];
//    [UIView animateWithDuration:0.4 animations:^{
//        [img setFrame:CGRectMake(SCREENWIDTH/2-50, 58, ImageWidth, 100)];
//        [dropDownTab setFrame:CGRectMake(5, 8, TableWidth, 92)];
//    }];
}

- (void)removeView
{
    [img setFrame:CGRectMake((SCREEN_WIDTH-ImageWidth)/2, 58, ImageWidth, 0)];
    [dropDownTab setFrame:CGRectMake(5, 8, TableWidth, 0)];
    [self removeFromSuperview];
//    [UIView animateWithDuration:0.4 animations:^{
//        [img setFrame:CGRectMake(SCREENWIDTH/2-50, 58, ImageWidth, 0)];
//        [dropDownTab setFrame:CGRectMake(5, 8, TableWidth, 0)];
//    } completion:^(BOOL finished) {
//        [self removeFromSuperview];
//    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return listArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellIdentifier = @"cellIdentifier";
    
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [cell setBackgroundColor:[UIColor clearColor]];
        cell.textLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:14];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = UIColorFromRGB(0x808080);
        
        UIView* bottomlineView = [[UIView alloc]initWithFrame:CGRectMake(5, 29.5, TableWidth-10, 0.5)];
        [bottomlineView setBackgroundColor:[UIColor colorWithRed:245/255 green:246/255 blue:247/255 alpha:0.2]];
        bottomlineView.tag = 200;
        [cell.contentView addSubview:bottomlineView];
    }
    cell.textLabel.text = [listArr objectAtIndex:indexPath.row];
    if (indexPath.row == celIndex) {
        
        cell.textLabel.textColor = mainColor;
    }
    
    UIView* lineView = (UIView*)[cell.contentView viewWithTag:200];
    if (indexPath.row == listArr.count-1) {
        lineView.hidden = YES;
    }else{
        lineView.hidden = NO;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.SelectBlock) {
        self.SelectBlock(indexPath.row);
    }
    [self removeView];
}

- (void)removeSelf
{
    if (self.SelectBlock) {
        self.SelectBlock(10000);
    }
    [self removeView];
}

@end
